< previous page page_667 next page >

Page 667
records whenever you delete a related record from the validation table. If the user attempts to delete a record from the validation table that is used by one or more records in the master table, Microsoft Jet issues an error message telling the user that it is unable to delete the record.
Whenever you define a relation object in which the base table is a master table and the foreign table is a child table (for example, CustomerMaster.CustID is the base table and CustomerComments.CustID is the foreign table), you might want to define both an update and a delete cascade. It is logical to make sure that any changes to the CustomerMaster.CustID field would be updated in the CustomerComments.CustID field. It also might make sense to delete all CustomerComments records whenever the related CustomerMaster record is deleted. This is not always the case, though. If the child table is CustomerInvoice, for example, you might not want to automatically delete all invoices on file. Instead, you might want Microsoft Jet to prevent the deletion of the CustomerMaster record if a related CustomerInvoice record exists.
The key point to remember is that cascades are performed automatically by Microsoft Jet, without any warning message. You cannot create an optional cascade or receive an automatic warning before a cascade begins. If you choose to use cascades in your database, be sure to think through the logic and the relations thoroughly, and be sure to test your relations and cascades before using the database in a production setting.
Transaction Management
Another important tool for maintaining the integrity of your database is the use of transactions to manage database updates and deletes. Visual Basic enables you to enclose all database update operations as a single transaction. Transactions involve two steps: First, mark the start of a database transaction with the BeginTrans keyword; second, mark the end of the database transaction with the CommitTrans or RollBack keyword. You can start a set of database operations (add, edit, and delete records) and then, if no error occurs, you can use the CommitTrans keyword to save the updated records to the database. If you encounter an error along the way, though, you can use the RollBack keyword to tell Microsoft Jet to reverse all database operations completed up to the point where the transaction first began.
Suppose that you need to perform a series of database updates to several tables as part of a month-end update routine for an accounting system. This month-end processing includes totaling transactions by customer from the TransTable, writing those totals to existing columns in a CustTotals table, appending the transactions to the HistoryTable, and deleting the transactions from the TransTable. The process requires access to three different tables and involves updating existing records (appending new records to a table and deleting existing records from a table). If your program encounters an error part of the way through this process, it will be difficult to reconstruct the data as it existed before the process began. In other words, it will be difficult unless you used Visual Basic transactions as part of the update routine.

 
< previous page page_667 next page >

If you like this book, buy it!